// pages/location/location.js
const app = require('../../utils/ajax.js');
const {
  weixin
} = require('../../utils/value.js');
const config = require('../../utils/config.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hospitalList: [],
    hospitalAllList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.handleGetHospitalList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  /**
   * 本地搜索医院
   */
  handleSearch: function(e) {
    const value = e.detail.value;
    const hospitalAllList = this.data.hospitalAllList;
    let matchData = [];
    if (value && value.length > 0) {
      for (let i = 0; i < hospitalAllList.length; i++) {
        let item = hospitalAllList[i];
        if (item.hospital_name.search(value) !== -1) {
          matchData.push(item);
        }
      }
    } else {
      matchData = hospitalAllList;
    }
    this.setData({
      hospitalList: matchData
    });
  },

  /**
   * 处理选择医院
   */
  handleChoose: function(e) {
    const index = e.currentTarget.id
    const hospitalList = this.data.hospitalList;
    const item = hospitalList[index];
    const hospital_id = item.hospital_id;
    const url = config.ServerUrl + weixin.SELECTIVE_LOCATION;
    app.doGet(url, {
      hospital_id: hospital_id,
      weixin_id: app.getData('open_id')
    }, function(res) {
      if (res.state === 1) {
        let advertise_image = '../image/u24.png';
        if (item.advertise_image) {
          advertise_image = config.imageServerUrl + item.advertise_image;
        }
        let hospital_picture = [];
        if (item.hospital_picture) {
          for (let i = 0; i < item.hospital_picture.length; i++) {
            hospital_picture[i] = config.imageServerUrl + item.hospital_picture[i];
          }
        } else {
          for(let i=0;i<3;i++){
            hospital_picture[i] = '../image/u17.png';
          }
        }
        const hospitalInfo = {
          hospital_name: item.hospital_name,
          hospital_id: item.hospital_id,
          detailaddress: item.detailaddress,
          advertise_image: advertise_image,
          hospital_picture
        }
        app.setData('hospitalInfo', hospitalInfo);
        wx.navigateBack({
          delta: 1
        });
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 1000
        })
      }
    })
  },

  /**
   * 从服务器上获取医院列表
   */
  handleGetHospitalList: function() {
    wx.showLoading({
      title: '数据加载中',
    })
    const _this = this;
    const url = config.ServerUrl + weixin.SELECT_HOSPITAL_ALL_MAG;
    app.doGet(url, {}, function(res) {
      console.log(res);
      if (res.state === 2) {
        _this.setData({
          hospitalAllList: res.data,
          hospitalList: res.data
        });
        wx.hideLoading();
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 1000
        })
        _this.setData({
          hospitalAllList: [],
          hospitalList: []
        });
      }
    })
  }
})